<template>
  <div class="course-info-display">
    <div v-if="loading">
      <el-skeleton :rows="4" animated />
    </div>
    
    <div v-else-if="course">
      <div class="course-header">
        <h1 class="course-title">{{ course.title }}</h1>
        <div class="course-meta">
          <el-tag type="info" size="small">课程ID: {{ course.id }}</el-tag>
          <el-tag type="success" size="small">讲师ID: {{ course.userid }}</el-tag>
        </div>
      </div>
      
      <div class="course-image-container" v-if="course.picPaths">
        <img 
          :src="course.picPaths" 
          :alt="course.title"
          class="course-image"
          @error="handleImageError"
        />
      </div>
      
      <div class="course-description">
        <h3>课程简介</h3>
        <p>{{ course.body || '暂无课程简介' }}</p>
      </div>
      
      <div class="course-schedule">
        <h3>课程时间</h3>
        <div class="time-info">
          <div class="time-item">
            <label>开始时间：</label>
            <span>{{ formatTime(course.startTime) }}</span>
          </div>
          <div class="time-item">
            <label>结束时间：</label>
            <span>{{ formatTime(course.endTime) }}</span>
          </div>
        </div>
      </div>
    </div>
    
    <div v-else class="error-container">
      <el-result
        icon="warning"
        title="课程信息加载失败"
        sub-title="请稍后重试"
      >
        <template #extra>
          <el-button type="primary" @click="$emit('reload')">重新加载</el-button>
        </template>
      </el-result>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import * as api from '../services/api'
import { ElMessage } from 'element-plus'

const props = defineProps({
  courseId: {
    type: [String, Number],
    required: true
  }
})

const emit = defineEmits(['reload'])

const course = ref(null)
const loading = ref(false)

const loadCourse = async () => {
  if (!props.courseId) return
  
  try {
    loading.value = true
    const response = await api.getCourseById(props.courseId)
    course.value = response
  } catch (error) {
    console.error('获取课程详情失败:', error)
    ElMessage.error('获取课程详情失败')
    course.value = null
  } finally {
    loading.value = false
  }
}

const formatTime = (timestamp) => {
  if (!timestamp) return '未设置'
  const date = new Date(parseInt(timestamp))
  return date.toLocaleString('zh-CN')
}

const handleImageError = (e) => {
  e.target.style.display = 'none'
}

watch(() => props.courseId, loadCourse)

onMounted(() => {
  loadCourse()
})
</script>

<style scoped>
.course-info-display {
  max-width: 800px;
}

.course-header {
  margin-bottom: 30px;
}

.course-title {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  margin: 0 0 10px 0;
}

.course-meta {
  display: flex;
  gap: 10px;
}

.course-image-container {
  margin-bottom: 30px;
}

.course-image {
  width: 100%;
  max-height: 400px;
  object-fit: cover;
  border-radius: 8px;
}

.course-description, .course-schedule {
  margin-bottom: 30px;
}

.course-description h3, .course-schedule h3 {
  font-size: 20px;
  color: #303133;
  margin-bottom: 15px;
  border-bottom: 1px solid #e4e7ed;
  padding-bottom: 8px;
}

.course-description p {
  font-size: 16px;
  line-height: 1.6;
  color: #606266;
  margin: 0;
}

.time-info {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.time-item {
  display: flex;
  align-items: center;
}

.time-item label {
  font-weight: bold;
  margin-right: 10px;
  min-width: 80px;
  color: #606266;
}

.time-item span {
  color: #303133;
}

.error-container {
  text-align: center;
  padding: 40px;
}
</style>